import osimport reimport timeimport sys
import OSCfrom threading import Thread
import socket

class OSCReciever(Thread):
    def __init__(self, callback):
        Thread.__init__(self)
        self.callback = callback
        self.running = True
        self.serversocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
#       self.serversocket.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
        self.serversocket.bind(("", 9999))
        
    def stop(self):    
    	print("Killing OSC thread")
        self.running = False
    def run(self):
        while self.running:
            data, addr = self.serversocket.recvfrom(1024)
            print addr
            message = OSC.decodeOSC(data)
            self.callback(message)
class OSCHandler(Thread):	def __init__ (self):
		self.val = 1
		self.data = [0,0,0]		Thread.__init__(self)	def run(self):		self.getOSC(8000)
      
	def getOSC(self, port):
		s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
		s.bind(('', port))
		while 1:
			self.data, address = s.recvfrom(1024)
		#	print address
		#	OSC.hexDump(self.data)
		#	print OSC.decodeOSC(self.data)
			OSC.decodeOSC(self.data)
			if self.val == 1:
				self.val = 0
			else:
				self.val = 1
	def getValue(self):
		#return OSC.decodeOSC(self.data)
		return self.val
		
	def setCallback(self, call):
		self.callback = call